package com.alipay.service;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AliPayProperties;
import com.alipay.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 调用支付宝支付的组件
 *
 * @Author axin
 * @Date 2021/12/4 11:20
 **/
@Component
public class Alipay {

    @Autowired
    private AliPayProperties aliPayProperties;

    /**
     * 支付接口
     *
     * @param order
     * @return
     */
    public String pay(Order order) {
        try{
            // 支付宝网关
            String serverUrl = aliPayProperties.getGatewayUrl();
            // APPID
            String appId = aliPayProperties.getAppId();
            // 商户私钥, 即PKCS8格式RSA2私钥
            String privateKey = aliPayProperties.getPrivateKey();
            // 格式化为 json 格式
            String format = "json";
            // 字符编码格式
            String charset = aliPayProperties.getCharset();
            // 支付宝公钥, 即对应APPID下的支付宝公钥
            String alipayPublicKey = aliPayProperties.getPublicKey();
            // 签名方式
            String signType = aliPayProperties.getSignType();
            // 页面跳转同步通知页面路径
            String returnUrl = aliPayProperties.getReturnUrl();
            // 服务器异步通知页面路径
            String notifyUrl = aliPayProperties.getNotifyUrl();

            // 1、获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(
                    serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);

            // 2、设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            // 页面跳转同步通知页面路径
            alipayRequest.setReturnUrl(returnUrl);
            // 服务器异步通知页面路径
            alipayRequest.setNotifyUrl(notifyUrl);
            // 封装参数(以json格式封装)
            alipayRequest.setBizContent(JSON.toJSONString(order));

            // 3、请求支付宝进行付款，并获取支付结果
            String result = alipayClient.pageExecute(alipayRequest).getBody();
            // 返回付款信息
            return result;

        } catch (AlipayApiException e){
            return null;
        }
    }
}
